package com.xhui.vimarket.common.utils.obj;

import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;

/**
 * @版权：福富软件 版权所有 (c) 2008
 * @文件：com.xhui.crm.common.util.obj.BeanUtilEx.java
 * @所含类：BeanUtilEx
 * @author: peilh
 * @version: V1.0
 * @see:
 * @创建日期：2008-8-23
 * @功能说明：
 * @修改记录： =============================================================<br>
 *        日期:2008-8-23 peilh 创建
 *        =============================================================<br>
 */
public class BeanUtilsExtend extends BeanUtils {
    private static final Logger logger = Logger.getLogger(BeanUtilsExtend.class);
    
    /**
     * 构造函数.
     */
    protected BeanUtilsExtend() {
    }
    
    static {
        // 注册sql.date的转换器，即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
        // 这个class，是目标对象的类型,
        ConvertUtils.register(new SqlDateConverter(), java.sql.Date.class);
        // ConvertUtils.register(new SqlTimestampConverter(),
        // java.sql.Timestamp.class);,to-do
        // 注册util.date的转换器，即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
        ConvertUtils.register(new UtilDateConverter(), java.util.Date.class);
        ConvertUtils.register(new StringConvert(), java.lang.String.class);
    }
    
    /**
     * copyProperties.
     * 
     * @param target Object
     * @param source Object
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static void copyProperties(final Object target, final Object source)
        throws InvocationTargetException, IllegalAccessException {
        org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
    }
    
    /**
     * cloneObject.
     * 
     * @param obj Object
     * @return Object
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static Object cloneObject(Object obj) {
        return obj;
    }
    
    /**
     * cloneObject1.
     * 
     * @param obj Object
     * @return Object
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static Object cloneObject1(Object obj)
        throws IllegalAccessException, InstantiationException, InvocationTargetException,
        NoSuchMethodException {
        Object newObj = org.apache.commons.beanutils.BeanUtils.cloneBean(obj);
        return newObj;
    }
    
    /**
     * cloneObject2.
     * 
     * @param object Object
     * @return Object
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static Object cloneObject2(Object object) {
        if (object == null) {
            return null;
        }
        try {
            Writer w = new java.io.StringWriter();
            org.exolab.castor.xml.Marshaller.marshal(object, w);
            
            Reader r = new java.io.StringReader(w.toString());
            return org.exolab.castor.xml.Unmarshaller.unmarshal(object.getClass(), r);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * getAttr.
     * 
     * @param obj Object
     * @param name String
     * @return Object
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static Object getAttr(Object obj, String name) {
        try {
            return PropertyUtils.getProperty(obj, name);
            // }
        } catch (IllegalAccessException e) {
            /**
             * wuyx(吴宇星) 11:17:18
                            不要抛异常出来
              chenmq(陈明乾) 11:17:37
                             那我过滤了、
              wuyx(吴宇星) 11:17:55
                            嗯
             */
            //  throw new RtManagerException("根据属性名称，读取属性值" + name, obj == null ? BeanUtilsExtend.class
            //      : obj.getClass(), "getAttr", "IllegalAccessException");
            String msg = "根据属性名称，读取属性值" + name + ",BeanUtilsExtend.getAttr";
            logger.error(msg);
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            //  throw new RtManagerException("根据属性名称，读取属性值" + name, obj == null ? BeanUtilsExtend.class
            //      : obj.getClass(), "getAttr", "InvocationTargetException");
            String msg = "根据属性名称，读取属性值" + name + ",BeanUtilsExtend.getAttr";
            logger.error(msg);
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            //  throw new RtManagerException("根据属性名称，读取属性值" + name, obj == null ? BeanUtilsExtend.class
            //     : obj.getClass(), "getAttr", "NoSuchMethodException");
            String msg = "根据属性名称，读取属性值" + name + ",BeanUtilsExtend.getAttr";
            logger.error(msg);
            e.printStackTrace();
        }
        //        catch (Exception e) {
        //            return "";
        //        }
        return "";
    }
    
    /**
     * isEqualsOfBean.
     * 
     * @param oldVal Object
     * @param value Object
     * @return boolean
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static boolean isEqualsOfBean(Object oldVal, Object value) {
        return !(value instanceof Collection)
            && (oldVal == value || (oldVal != null && oldVal.equals(value)) || (value != null && value
                .equals(oldVal)));
        
    }
    
    /**
     * 是否存在属性.
     * 
     * @param obj Object
     * @param name String
     * @return boolean
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public static boolean isExitProp(Object obj, String name) {
        try {
            PropertyUtils.getProperty(obj, name);
            return true;
        } catch (Exception e) {
        }
        return false;
    }
}
